using System;

namespace BEGDC.CC.Entity
{
	/// <summary>
	/// Helper class to encrypt price related number.
	/// </summary>
	/// <remarks>
	/// cause the hibernate do not read the property when it's null, so here's some hard code to avoid 0 to be decrypted to 16777215.
	/// 1. do not encrypt 0
	/// 2. decrypt the number encrypted by 10015218687.01 will get 16777215 as result.
	/// </remarks>
	public class EncryptHelper
	{
		public static decimal Encrypt(decimal number)
		{
			if(number == decimal.Zero) return decimal.Zero;
			if(number == 16777215M) return 10000000000.01M;
			Int64 x = decimal.ToInt64(number);
			decimal d = number - (decimal)x;
			x ^= 0xFFFFFF;
			d += (decimal) x;

			return d;
		}

		public static decimal Decrypt(decimal number)
		{
			if(number == decimal.Zero) return decimal.Zero;
			if(number == 10000000000.01M) return 16777215M;
			Int64 x = decimal.ToInt64(number);
			decimal d = number - (decimal)x;
			x ^= 0xFFFFFF;
			d += (decimal) x;

			return d;
		}
	}
}
